﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace DoorAndWindow
{
	public partial class DlgAddType : System.Windows.Forms.Form
	{
		public DlgAddType(UIApplication uiApp, BuiltInCategory bCategory)
		{
			DlgAddType.bc = bCategory;
			if (bCategory == BuiltInCategory.OST_Windows)
			{
                this.height = BuiltInParameter.FAMILY_HEIGHT_PARAM;
                this.width = BuiltInParameter.FAMILY_WIDTH_PARAM;
			}
			else if (bCategory == BuiltInCategory.OST_Doors)
			{
                this.height = BuiltInParameter.FAMILY_HEIGHT_PARAM;
                this.width = BuiltInParameter.FAMILY_WIDTH_PARAM;
			}
			this.m_application = uiApp;
			this.m_document = uiApp.ActiveUIDocument.Document;
			this.InitializeComponent();
			this.InitComboFamily();
			this.ResetComboType();
		}

		private void ResetComboType()
		{
			if (this.cmbType.Items.Count > 0)
			{
				this.cmbType.Items.Clear();
			}
			if (this.cmbFamily.Items.Count == 0)
			{
				return;
			}
			string familyName = this.cmbFamily.SelectedItem.ToString();
			IList<Element> list = ElementFiltering.FindTypesInFamily(this.m_document, typeof(FamilySymbol), familyName, DlgAddType.bc);
			foreach (Element element in list)
			{
				this.cmbType.Items.Add(element.Name);
			}
			if (this.cmbType.Items.Count > 0)
			{
				this.cmbType.SelectedIndex = 0;
			}
		}

		private void InitComboFamily()
		{
			if (this.cmbFamily.Items.Count > 0)
			{
				this.cmbFamily.Items.Clear();
			}
			IList<Element> list = ElementFiltering.FindSpecialElements(this.m_document, typeof(FamilySymbol), DlgAddType.bc);
			foreach (Element element in list)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				string text = familySymbol.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString();
				if (!this.cmbFamily.Items.Contains(text))
				{
					this.cmbFamily.Items.Add(text);
				}
			}
			if (this.cmbFamily.Items.Count > 0)
			{
				this.cmbFamily.SelectedIndex = 0;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
			base.Dispose();
		}

		private void btnCreate_Click(object sender, EventArgs e)
		{
			try
			{
				string familyName = this.cmbFamily.SelectedItem.ToString();
				string typeName = this.cmbType.SelectedItem.ToString();
				FamilySymbol familySymbol = ElementFiltering.FindElementType(this.m_document, typeof(FamilySymbol), familyName, typeName, DlgAddType.bc) as FamilySymbol;
				Family family = familySymbol.Family;
				Document document = this.m_document.EditFamily(family);
				if (document != null && document.IsFamilyDocument)
				{
					FamilyManager familyManager = document.FamilyManager;
					if (familyManager != null)
					{
						Transaction transaction = new Transaction(document, "CreateDoorFamilyTransaction");
						transaction.Start();
						SubTransaction subTransaction = new SubTransaction(document);
						subTransaction.Start();
						FamilyParameter familyParameter = familyManager.get_Parameter(this.width);
						if (familyParameter != null)
						{
							familyManager.Set(familyParameter, AssistFunc.mmToFeet(Convert.ToDouble(this.tbxWidth.Text.Trim())));
						}
						familyParameter = familyManager.get_Parameter(this.height);
						if (familyParameter != null)
						{
							familyManager.Set(familyParameter, AssistFunc.mmToFeet(Convert.ToDouble(this.tbxHeight.Text.Trim())));
						}
						subTransaction.Commit();
						transaction.Commit();
						document.Close(false);
						document.Dispose();
					}
				}
				TaskDialog.Show("添加门族类型", "族类型创建成功!");
				this.tbxType.Text = "";
				this.tbxWidth.Text = "";
				this.tbxHeight.Text = "";
				this.InitComboFamily();
				this.ResetComboType();
			}
			catch
			{
				TaskDialog.Show("添加门族类型", "族类型创建失败!");
			}
		}

		private void DlgAddDoorType_Load(object sender, EventArgs e)
		{
		}

		private void cmbDoorType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.ResetPreview();
		}

		private void ResetPreview()
		{
			this.pbPreview.Image = this.GetPreview(new Size(150, 150), this.cmbFamily.SelectedItem.ToString(), this.cmbType.SelectedItem.ToString());
		}

		private Bitmap GetPreview(Size size, string familyName, string familySymbolName)
		{
			Bitmap result = new Bitmap(size.Width, size.Height);
			FamilySymbol familySymbol = ElementFiltering.FindElementType(this.m_document, typeof(FamilySymbol), familyName, familySymbolName, DlgAddType.bc) as FamilySymbol;
			try
			{
				result = familySymbol.GetPreviewImage(size);
			}
			catch (Exception)
			{
			}
			return result;
		}

		private void cmbDoorFamily_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.ResetComboType();
		}

		private UIApplication m_application;

		private Document m_document;

		private static BuiltInCategory bc;

		private BuiltInParameter width;

		private BuiltInParameter height;
	}
}
